<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>欢迎页面</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 20px;
            background-color: #f5f5f5;
            min-height: 100vh;
        }
        .container {
            max-width: 1200px;
            margin: 0 auto;
            background-color: rgba(255, 255, 255, 0.9);
            padding: 20px;
            border-radius: 5px;
            box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
            backdrop-filter: blur(10px);
        }
        .navbar {
            background-color: rgba(51, 51, 51, 0.95);
            overflow: hidden;
            margin-bottom: 20px;
            border-radius: 5px;
            box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
        }
        .navbar a {
            float: left;
            display: block;
            color: white;
            text-align: center;
            padding: 14px 16px;
            text-decoration: none;
            transition: all 0.3s;
        }
        .navbar a:hover {
            background-color: rgba(221, 221, 221, 0.9);
            color: black;
        }
        .navbar a.active {
            background-color: #4CAF50;
        }
        .header-actions {
            margin: 20px 0;
            text-align: right;
        }
        .header-actions button {
            margin-left: 10px;
            padding: 8px 12px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            transition: all 0.3s;
        }
        .header-actions button:hover {
            background-color: #45a049;
            transform: translateY(-1px);
        }
        .header-actions button:last-child {
            background-color: #f44336;
        }
        .header-actions button:last-child:hover {
            background-color: #d32f2f;
        }
        .welcome-content {
            text-align: center;
            padding: 50px 0;
        }
        h1 {
            text-align: center;
            color: #1a237e;  /* 深蓝色 */
            margin-bottom: 30px;
            font-size: 2.5em;
            text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.1);
        }
        .welcome-content p {
            color: #666;
            font-size: 18px;
            margin-bottom: 30px;
        }
        .user-info {
            background-color: rgba(255, 255, 255, 0.8);
            padding: 20px;
            border-radius: 5px;
            margin-bottom: 30px;
            text-align: left;
            border: 1px solid rgba(0, 0, 0, 0.05);
            box-shadow: 0 2px 5px rgba(0, 0, 0, 0.05);
        }
        .user-info p {
            margin: 10px 0;
            font-size: 16px;
            color: #555;
        }
        .quick-links {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
            gap: 20px;
            margin-top: 30px;
        }
        .quick-link {
            padding: 20px;
            background-color: #e9ecef;
            border-radius: 5px;
            text-decoration: none;
            color: #333;
            text-align: center;
            transition: all 0.3s;
            border: 1px solid #dee2e6;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
        }
        .quick-link:hover {
            background-color: #4CAF50;
            color: white;
            transform: translateY(-2px);
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            border-color: transparent;
        }
        .quick-link h3 {
            margin: 0 0 10px 0;
            color: inherit;
        }
        .quick-link p {
            margin: 0;
            font-size: 14px;
            color: inherit;
            opacity: 0.9;
        }
        /* 登录框样式 */
        .login-modal {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, 0.5);
            z-index: 1000;
            justify-content: center;
            align-items: center;
        }
        .login-modal.show {
            display: flex;
        }
        .login-container {
            background-color: white;
            padding: 20px;
            border-radius: 5px;
            box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
            width: 300px;
        }
        .form-group {
            margin-bottom: 15px;
        }
        .form-group label {
            display: block;
            margin-bottom: 5px;
            color: #333;
        }
        .form-group input {
            width: 100%;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
            transition: border-color 0.3s;
        }
        .form-group input:focus {
            border-color: #4CAF50;
            outline: none;
        }
        .form-actions {
            display: flex;
            justify-content: space-between;
            margin-top: 20px;
        }
        .form-actions button {
            padding: 8px 16px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            transition: all 0.3s;
        }
        .form-actions button.cancel {
            background-color: #ddd;
        }
        .form-actions button.cancel:hover {
            background-color: #ccc;
        }
        .error-message {
            color: #f44336;
            margin-top: 10px;
            display: none;
            font-size: 14px;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header-actions">
            <button onclick="showLoginModal()">切换用户</button>
            <button onclick="logout()">退出登录</button>
        </div>
        
        <div class="welcome-content">
            <h1>欢迎使用系统</h1>
            
            <div class="user-info">
                <p><strong>当前用户：</strong><span id="currentUsername">-</span></p>
                <p><strong>用户角色：</strong><span id="currentRole">-</span></p>
            </div>
            
            <p>请选择要进入的功能模块：</p>
            
            <div class="quick-links">
                <a href="/monitor.html" class="quick-link">
                    <h3>实时监控</h3>
                    <p>查看系统运行状态</p>
                </a>
                <a href="/config.html" class="quick-link" id="configQuickLink" style="display: none;">
                    <h3>系统配置</h3>
                    <p>管理系统配置</p>
                </a>
                <a href="/log.html" class="quick-link">
                    <h3>系统日志</h3>
                    <p>查看系统日志记录</p>
                </a>
                <a href="/music.html" class="quick-link">
                    <h3>音乐播放</h3>
                    <p>播放系统音乐</p>
                </a>
            </div>
        </div>
    </div>
    
    <!-- 登录框 -->
    <div id="loginModal" class="login-modal">
        <div class="login-container">
            <h2>用户登录</h2>
            <form id="loginForm" onsubmit="switchUser(event)">
                <div class="form-group">
                    <label for="username">用户名</label>
                    <input type="text" id="username" name="username" required>
                </div>
                <div class="form-group">
                    <label for="password">密码</label>
                    <input type="password" id="password" name="password" required>
                </div>
                <div id="loginError" class="error-message" style="display: none;"></div>
                <div class="form-actions">
                    <button type="button" onclick="hideLoginModal()">取消</button>
                    <button type="submit">登录</button>
                </div>
            </form>
        </div>
    </div>
    
    <script>
        document.addEventListener('DOMContentLoaded', function() {
            // 页面加载时检查用户信息
            const username = localStorage.getItem('username');
            const userRole = localStorage.getItem('userRole');
            
            if (!username || !userRole) {
                window.location.href = '/login.html';
                return;
            }
            
            document.getElementById('currentUsername').textContent = username;
            document.getElementById('currentRole').textContent = userRole === 'admin' ? '管理员' : '普通用户';
            
            // 根据用户角色显示/隐藏配置入口
            if (userRole === 'admin') {
                document.getElementById('configQuickLink').style.display = 'block';
            }

            // 设置当前活动页面的样式
            const currentPath = window.location.pathname;
            document.querySelectorAll('.navbar a').forEach(link => {
                if (link.getAttribute('href') === currentPath) {
                    link.classList.add('active');
                } else {
                    link.classList.remove('active');
                }
            });
        });
        
        // 显示登录框
        function showLoginModal() {
            document.getElementById('loginModal').classList.add('show');
            // 根据当前用户设置提示的用户名
            const currentUser = localStorage.getItem('username');
            const usernamePlaceholder = currentUser === 'admin' ? 'user' : 'admin';
            document.getElementById('username').placeholder = usernamePlaceholder;
            document.getElementById('username').value = '';
            document.getElementById('password').value = '';
            document.getElementById('loginError').style.display = 'none';
            document.getElementById('loginError').textContent = '用户名或密码错误';
        }
        
        // 隐藏登录框
        function hideLoginModal() {
            document.getElementById('loginModal').classList.remove('show');
            document.getElementById('loginError').style.display = 'none';
            document.getElementById('loginForm').reset();
        }
        
        // 切换用户
        async function switchUser(event) {
            // 阻止表单默认提交行为
            if (event) {
                event.preventDefault();
            }
            
            const username = document.getElementById('username').value;
            const password = document.getElementById('password').value;
            
            try {
                const response = await fetch('/api/login', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({ username, password })
                });
                
                if (response.ok) {
                    const data = await response.json();
                    if (data.role === 'user') {
                        localStorage.setItem('userRole', 'user');
                    } else {
                        localStorage.setItem('userRole', 'admin');
                    }
                    localStorage.setItem('username', data.username);
                    
                    // 刷新页面以更新显示
                    window.location.reload();
                } else {
                    document.getElementById('loginError').style.display = 'block';
                }
            } catch (error) {
                console.error('登录失败:', error);
                document.getElementById('loginError').style.display = 'block';
            }
        }
        
        // 退出登录
        async function logout() {
            try {
                const response = await fetch('/api/logout', {
                    method: 'POST'
                });
                
                if (response.ok) {
                    localStorage.removeItem('userRole');
                    localStorage.removeItem('username');
                    window.location.href = '/login.html';
                } else {
                    alert('退出失败: ' + (await response.text()));
                }
            } catch (error) {
                alert('退出失败: ' + error.message);
            }
        }
    </script>
</body>
</html> 